<html>
<head>
  <title>How To Make Subroutines Callee-Save...</title>
</head>

<body>

<!-- BEGIN LAB DESCRIPTION -->

<font face="Arial">
<!------------------------------------------------------------------------------------>
<a name="HowToCalleeSave">
<h2>How To Make Subroutines Callee-Save...</h2>
</a>

<p>Any register used by a subroutine <b>MUST</b> be saved and restored from the stack!
The following routine illustrates this procedure:

<p><blockquote><pre>;------------------------------------------------------------------------------
; SUBROUTINE: GENERATES NEXT RANDOM NUMBER
;
;    OUT:    r12 = 0-32767
;            random seed updated
;
rand16: push    r4                     ; save registers
        push    r5
        push    r6
        push    r7

        mov.w   RandSeed,r5            ; Prepare multiplication
        mov.w   #MULT,r4               ; Prepare multiplication
        call    #MPYU                  ; Call unsigned MPY (5.1.1)
        add.w   #INC,r7                ; Add INC to low word of product
        mov.w   r7,RandSeed            ; Update RandSeed
        mov.w   r7,r12                 ; return in r12
        swpb    r12

        pop     r7                     ; restore registers
        pop     r6
        pop     r5
        pop     r4
        ret                            ; Random number in Rndnum</pre></blockquote>


<!-- END LAB DESCRIPTION -->

</body>
</html>
